<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class EXT_Controller extends CI_Controller
{
	public $client = false;
	public $user = null;
	public $router_class = null;
	public $router_method = null;
	function __construct()
	{
		parent::__construct();
		$this->load->config('common');
		// Debug
		$this->output->enable_profiler(TRUE);

		$this->lang->load('user');
		$this->load->model('user_model');
		$this->user = $this->user_model->user;
		$this->router_class = $this->router->fetch_class();
		$this->router_method = $this->router->fetch_method();

		$this->load->library('userdata', array('name'=>$this->session->cookie_prefix.'return'), 'return');

		$current_url = $this->return->get('current');
		$new_current_url = current_url();
		if($current_url <> $new_current_url)
		{
			$this->return->set(array('last'=>$current_url, 'current'=>$new_current_url))->store();
		}
	}
}

/**
 * Back end Controller
 * 
 */
class BackendController extends EXT_Controller
{
	public $filter_name;
	function __construct()
	{
		parent::__construct();
		$this->load->config('backend');
		if(!$this->user_model->loggedIn())
		{
			redirect(siteUrl('user/login'));
		}
		if(!$this->user_model->isAdmin())
		{
			$this->messages->add(lang('error_forbidden'), 'e');
			redirect(base_url());
		}

		$this->filter_name = 'filter';
		if(!$this->client)
		{
			$this->filter_name .= '_admin';
		}
		$this->filter_name .= '_'.$this->router_class;
		$this->load->library('userdata', array('name'=>$this->filter_name), 'filter');
	}
	public function clear_filter()
	{
		$this->filter->clear();
		redirect($this->return->get('last'));
	}
}

/**
 * Front end Controller
 * 
 */
class FrontendController extends EXT_Controller
{
	function __construct()
	{
		parent::__construct();
		$this->load->config('frontend');
		$this->client = true;

		$this->load->model('category_model');
		$this->load->model('post_model');

		if(!$this->input->get('ajax'))
		{
			$this->output->set_template('default');
			$this->output->set_common_meta(lang('site_name'), lang('site_description'), lang('site_keywords'));
			$temp = 'items';
		}
	}
}


